﻿using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysService;
using JshRCSWebApi.Service.SysService;
using JshRCSWebApi.SqlSugarDB.Model.jshrcs_initial;
using SqlSugar.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JshRCSWebApi.ServiceImpl.SysService
{
    public class LogServiceImpl : LogService
    {

        private readonly Repository<dynamic> _repository;

        public LogServiceImpl(Repository<dynamic> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public async Task<ApiResult> DeleteLog(int ID)
        {
            int count = _repository.Delete<servicelog>(sl => sl.ID == ID);
            return await Task.Run(async () =>
                 count > 0 ? ApiResult.Success("删除成功") : ApiResult.Error("删除失败"));
        }

        /// <summary>
        /// 获取日志集合
        /// </summary>
        /// <param name="searchDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> GetLogList(LogSearchDTO searchDTO)
        {
            int total = 0;
            var LogList = _repository.Context.Queryable<servicelog>()
                                       .WhereIF(!string.IsNullOrEmpty(searchDTO.slType), sl => sl.slType == searchDTO.slType)
                                       .WhereIF(!string.IsNullOrEmpty(searchDTO.IpAddress), sl => sl.slIPAddress == searchDTO.IpAddress)
                                       .WhereIF(searchDTO.startTime != default(DateTime) && searchDTO.endTime != default(DateTime), sl => sl.CreateTime >= searchDTO.startTime && sl.CreateTime <= searchDTO.endTime)
                                       .OrderByDescending(sl => sl.CreateTime)
                                       .ToPageList(searchDTO.pageNo, searchDTO.pageSize, ref total)
                                       .Select(sl => new
                                       {
                                           ID = sl.ID,
                                           slType = sl.slType,
                                           slTitle = sl.slTitle,
                                           slContent = sl.slContent,
                                           slTableName = sl.slTableName,
                                           slIPAddress = sl.slIPAddress,
                                           slSUName = _repository.Select<serviceuser>(su => su.ID == sl.CreateBy)?.suName ?? string.Empty,
                                           CreateBy = sl.CreateBy,
                                           CreateTime = sl.CreateTime
                                       }).ToList();
            return await Task.Run(async () =>
                 ApiResult.Success("查询成功", new { LogList = LogList, total = total }));
        }
    }
}
